<?php
/**
  * ################################################# *
  * ################################################# *
  * Simple banner rotator class, created by bloodse3ker *
  * ################################################# *
  * ################################################# *
**/
class Banners {

 /*----- ?????????? ?? ???????????? -----*/
 
 var $connection;
 var $bannerURL = array();
 var $bannerIMG = array();
 var $bannerDescription = array();
 var $num_banners;
 var $rand_banner;
 
 /*----- Class ??????????? -----*/
 
 function Banners() {
  // ?????????? ?? ???????, ????? ?? ???????? ? ?????? ?????
  define("DB_HOST","localhost");
  define("DB_USER","root");
  define("DB_PASS","ascnet");
  define("DB_NAME","cms");
  define("TBL_BANNERS","banners");
  // ????????? ?? ???????? ? ?????? ?????
  $this->connection = mysql_connect(DB_HOST, DB_USER, DB_PASS) or die(mysql_error());
  mysql_select_db(DB_NAME, $this->connection) or die(mysql_error());
  // ???????? ?? ???????????? 'num_banners'
  $this->num_banners = 0;
  // ??????????? ?? ?????????
  $this->updateArray();
  $this->rotateBanner();
 }
 /*----- ???????? ???? ?? ???????? ? ????????? -----*/
 
 function calcNumBanners() {
  $q = "SELECT * FROM ".TBL_BANNERS;
  $result = mysql_query($q, $this->connection);
  $this->num_banners = mysql_numrows($result);  
 }
 /*----- ????????? ???????? ? ???????? -----*/
 
 function updateArray() {
  // ???????? ???? ?? ????????
  $this->calcNumBanners();
  // ????? ???????????? ?? ???????? ?? ??
  $q = "SELECT * FROM ".TBL_BANNERS;
  $result = mysql_query($q, $this->connection);
  // ??????? ???????????? ?? ???????? ?? ??????
  for ($i=0;$i < $this->num_banners;$i++) {
   $this->bannerURL[$i] = mysql_result($result, $i, 'url');
   $this->bannerIMG[$i] = mysql_result($result, $i, 'image_url');
   $this->bannerDescription[$i] = mysql_result($result, $i, 'description');
  }
 }
 /*----- ?????? ?????????? ????? -----*/
 
 function rotateBanner() {
  // ?????? ?????????? ?????
  $random = rand (0, ($this->num_banners) - 1);
  // ????????? ???????????? ?? ??????????? ????? ? ???? ??????????
  $this->rand_banner = $this->bannerURL[$random]."|".$this->bannerIMG[$random]."|".$this->bannerDescription[$random];
 }
 /*----- ??????? ?????? -----*/
 
 function showBanner() {
  // ?????? ?????????? ?????
  $this->rotateBanner();
  // ???????? ???? ??????????? ?????? ? ??
  if (empty($this->bannerURL) || empty($this->bannerIMG) || empty($this->bannerDescription)) {
   // ??? ????
   echo "No banners avaivable.";
  }
  else {
   // ??????? ???????????? ?? ?????? ?? '?????? ??????????'
   $banner = explode("|", $this->rand_banner);
 
   echo "<a href='$banner[0]'><img src='$banner[1]' alt='$banner[2]'/></a>";
  }
 }
 /*----- ???????? ?? ??? ????? -----*/
 
 function addBanner($url, $image, $description) {
  // ???????? ???????? ?????? ?????????? ? ??
  $q = "INSERT INTO ".TBL_BANNERS." VALUES('$url', '$image', '$description')";
  return mysql_query($q, $this->connection);
  // ????????? ????????
  $this->updateArray();
 }
 /*----- ????????? ?? ??????????? ????? -----*/
 
 function Exists($url, $image, $description) {
  // ??????
  $q = "SELECT * FROM ".TBL_BANNERS." WHERE `url` = '$url' OR `image_url` = '$image' OR `description` = '$description'";
  $result = mysql_query($q, $this->connection);
  // ??? ???????? ????? ??????????? ????????
  if (mysql_num_rows($result) > 0) {
   return 1;
  }
  else {
   // ??? ???? ?????? -> ????????
   return 0;
  }
 }
};
// ??????? ?????? 'banner'
$banner = new Banners();
?>